'use strict';
createjs.setInterval = function(t, e) {
  var i = new createjs.Container, s = createjs.Tween.get(i).wait(e).call(function() {
      t && t();
    });
  return s.loop = 1, s;
}, createjs.clearInterval = function(t) {
  createjs.Tween.removeTweens(t.target), t = void 0;
};
var Utils = {
    throttle: function(t, e, i) {
      e || (e = 200);
      var s = null, r = null;
      return function() {
        var a, n = i || this, o = +new Date, h = arguments;
        s && o < s + e ? (a = e - (o - s), clearTimeout(r), r = setTimeout(function() {
          s = o, t.apply(n, h);
        }, a)) : (s = o, t.apply(n, h));
      };
    },
  getRandom: function(t, e) {
      return Math.random() * (e - t) + t;
    },
  getRandomInt: function(t, e) {
      return Math.floor(Utils.getRandom(t, e));
    },
  createRandomOneZero: function(t) {
      if (1 === t) return Utils.getRandomInt(0, 2);
      for (var e = [], i = 0; i < t; i++) e.push(Utils.getRandomInt(0, 2));
      return e;
    },
  shuffle: function(t) {
      for (var e = t.length; e > 0; e--) {
        var i = Utils.getRandomInt(0, e);
        [t[e - 1], t[i]] = [t[i], t[e - 1]];
      }
      return t;
    },
  getGCD: function(t, e) {
      var i, s, r;
      for (i = t > e ? t : e, s = t < e ? t : e; i % s;) r = i % s, i = s, s = r;
      return s;
    },
  getLCM: function(t) {
      for (var e = 1; e < t.length; e++) {
        var i = t[0], s = t[e], r = Utils.getGCD(i, s);
        t[0] = i * s / r;
      }
      return t[0];
    },
  getFracDeno: function(t, e) {
      for (var i = 1, s = 1; Math.abs(i / s - t) > e;) i / s > t ? s++ : i / s < t && i++;
      return s;
    },
  createRandomInt_1: function(t, e) {
      for (var i = [], s = 0; s < t.length; s++) i[s] = Utils.getFracDeno(t[s], .001);
      for (var r = [], a = Utils.getLCM(i), n = 0, o = 0; o < i.length; o++) for (var h = a * i[o] + n; n < h;) r[n] = o, n++;
      if (1 === e) {
      var c = Utils.getRandomInt(0, r.length);
      return r[c];
    }
      for (var l = [], d = 0; d < e; d++) {
      var g = Utils.getRandomInt(0, r.length);
      l.push(r[g]);
    }
      return l;
    },
  createRandomInt_2: function(t, e) {
    if (1 === e) return Utils.shuffle(t), t[0];
    for (var i = [], s = 0; s < e; s++) Utils.shuffle(t), i.push(t[0]);
    return i;
  },
  getOppoPos: function(t, e, i) {
    return -(t - i + e);
  }
  },
  Game = {
    init: function(t) {
      var e = this;
      this.config = {
        canvas: 'J_game', initStairs: 8, distProb: [.5, .2, .2, .1], onGameEnd: function(t) {
        }
      };
      for (var i in t) this.config[i] = t[i];
      this.canvas = document.getElementById(this.config.canvas), this.stage = new createjs.Stage(this.canvas);
      var s = document.documentElement.clientWidth, r = document.documentElement.clientHeight;
      this.stageWidth = 2 * s, this.stageHeight = 2 * r, this.canvas.setAttribute('width', this.stageWidth), this.canvas.setAttribute('height', this.stageHeight), createjs.Ticker.setFPS(60), createjs.Ticker.setPaused(!0), createjs.Ticker.addEventListener('tick', function(t) {
        t.paused || e.stage.update();
      });
      var a = navigator.userAgent, n = parseFloat(a.substr(a.indexOf('Android') + 8, 3));
      (/Android/.test(a) && n > 4.3 || !/Android/.test(a)) && (createjs.Ticker.timingMode = createjs.Ticker.RAF), this.areaThreshold = this.stageWidth / 2, this.isAndroid = a.indexOf('Android') > -1 || a.indexOf('Adr') > -1, this.isStart = !1;
    },
  run: function() {
      this.stairSerial = Utils.createRandomOneZero(this.config.initStairs), this.barrSerial = Utils.createRandomInt_1(this.config.distProb, this.config.initStairs), this.clickTimes = 0, this.dropInterval = null, this.gameScore = 0, createjs.Ticker.setPaused(!1), this._createScene(), this._bindEvents();
    },
  start: function() {
      this.isStart = !0;
    },
  restart: function() {
      this.stage.clear(), this.run(), this.start();
    },
  loader: function(t, e) {
      var i = [{
          id: 'spriterobot',
          src: '//jdc.jd.com/demo/ting/H5Game/jumping/images/spriterobot@2x_62f169c2.png'
        }, {
          id: 'spritestairs',
          src: '//jdc.jd.com/demo/ting/H5Game/jumping/images/spritestairs@2x_93b62155.png'
        }, {
          id: 'leaf_left',
          src: '//jdc.jd.com/demo/ting/H5Game/jumping/images/leaf_left@2x_98e55a9f.png'
        }, {id: 'leaf_right', src: '//jdc.jd.com/demo/ting/H5Game/jumping/images/leaf_right@2x_1ddeb6b9.png'}],
        s = new createjs.LoadQueue((!0));
      s.loadManifest(i), s.on('complete', function() {
        e && e();
      }), s.on('fileload', function(e) {
        var i = parseInt(100 * e.target.progress) + '%';
        t && t(i);
      }), this.loader = s;
    },
  _createScene: function() {
      this.sceneBackground = new createjs.Shape, this.sceneBackground.graphics.beginFill('#001605').drawRect(0, 0, this.stageWidth, this.stageHeight), this.Leaves = new Leaves({transThreshold: this.stageHeight}), this.Floor = new Floor({
        stairSerial: this.stairSerial,
        barrSerial: this.barrSerial
      }), this.Robot = new Robot({initDirect: this.stairSerial[0]}), this.Stairs = new createjs.Container, this.Stairs.addChild(this.Floor.sprite, this.Robot.sprite), this.Stairs.lastX = this.Stairs.x, this.Stairs.lastY = this.Stairs.y, this.stage.addChild(this.sceneBackground, this.Stairs, this.Leaves.sprite);
    },
  _bindEvents: function() {
      var t = this;
      createjs.Touch.enable(this.stage);
      var e = Utils.throttle(this._handleUserClick, 70, t);
      this.sceneBackground.addEventListener('mousedown', e.bind(t));
    },
  _handleUserClick: function(t) {
      if (this.isStart) {
      this._autoDropStair();
      var e = t.stageX, i = e >= this.areaThreshold ? 1 : 0, s = i ? 75 : -75, r = 100;
      i ? this.Robot.right() : this.Robot.left(), this.Robot.move(s, r), this.Leaves.move(r), this._centerStairs(s, r), this._addStair();
      var a = this._checkUserAction(i);
      switch (a) {
        case 'barr':
          this._shakeStairs(), this.Robot.hitAndDisappear(), this._gameOver();
          break;
        case 'drop':
          this.Robot.dropAndDisappear(i), this._gameOver();
          break;
        case 'pass':
          this.gameScore++;
      }
    }
    },
  _gameOver: function() {
    this._clearAutoDropStair(), this.isStart = !1, this.config.onGameEnd(this.gameScore), setTimeout(function() {
      createjs.Ticker.setPaused(!0);
    }, 1e3);
  },
  _centerStairs: function(t, e) {
    this.Stairs.lastX -= t, this.Stairs.lastY += e, createjs.Tween.removeTweens(this.Stairs), createjs.Tween.get(this.Stairs, {override: !0}).to({
      x: this.Stairs.lastX,
      y: this.Stairs.lastY
    }, 500);
  },
  _shakeStairs: function() {
    createjs.Tween.removeTweens(this.Stairs), createjs.Tween.get(this.Stairs, {override: !0}).to({
      x: this.Stairs.x + 5,
      y: this.Stairs.y - 5
    }, 50, createjs.Ease.getBackInOut(2.5)).to({
      x: this.Stairs.x,
      y: this.Stairs.y
    }, 50, createjs.Ease.getBackInOut(2.5)).to({
      x: this.Stairs.x + 5,
      y: this.Stairs.y - 5
    }, 50, createjs.Ease.getBackInOut(2.5)).to({
      x: this.Stairs.x,
      y: this.Stairs.y
    }, 50, createjs.Ease.getBackInOut(2.5)).pause(), this.isAndroid && (window.navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate, window.navigator.vibrate([100, 30, 100, 30, 100, 200, 200, 30, 200, 30, 200, 200, 100, 30, 100, 30, 100]), window.navigator.vibrate(0));
  },
  _addStair: function() {
    var t = Utils.createRandomOneZero(1), e = Utils.createRandomInt_1(this.config.distProb, 1), i = !0;
    this.stairSerial.push(t), this.barrSerial.push(e), this.Floor.add(t, e, i);
  },
  _autoDropStair: function() {
    if (!this.dropInterval) {
      var t = this;
      this.dropInterval = createjs.setInterval(function() {
        t.Floor.drop(), t.Floor.dropIndex == t.clickTimes && (createjs.clearInterval(t.dropInterval), t.Robot.dropAndDisappear(), t._gameOver());
      }, 500);
    }
  },
  _clearAutoDropStair: function() {
    createjs.clearInterval(this.dropInterval);
  },
  _checkUserAction: function(t) {
    var e = '';
    return this.stairSerial[this.clickTimes] !== t ? (e = 1 === this.barrSerial[this.clickTimes] ? 'barr' : 'drop', this._clearAutoDropStair()) : e = 'pass', console.log(e), this.clickTimes++, e;
  }
  },
  Robot = function(t) {
    this.config = {initDirect: []};
    for (var e in t) this.config[e] = t[e];
    this.lastDirect = -1, this.lastPosX = 0, this.lastPosY = 0, this.init();
  };
Robot.prototype.init = function() {
  var t = new createjs.SpriteSheet({
    images: [Game.loader.getResult('spriterobot')],
    frames: {width: 150, height: 294, count: 21},
    animations: {walk: [0, 9, 'walk', .2], jump: [10, 16, 0, .5]}
  });
  this.sprite = new createjs.Sprite(t), this.lastPosX = this.sprite.x = (Game.stageWidth - this.sprite.getBounds().width) / 2 + 40, this.lastPosY = this.sprite.y = Utils.getOppoPos(this.sprite.getBounds().height, 350, Game.stageHeight), this.config.initDirect && this.right(!1);
},
  Robot.prototype.move = function(t, e) {
  this.lastPosX = this.lastPosX + t, this.lastPosY = this.lastPosY - e, this.sprite.gotoAndPlay('jump'), createjs.Tween.get(this.sprite).to({
    x: this.lastPosX,
    y: this.lastPosY
  }, 200);
},
  Robot.prototype.right = function() {
  1 !== this.lastDirect && (this.sprite.regX = 145, this.sprite.scaleX = -1, this.lastDirect = 1);
},
  Robot.prototype.left = function() {
  0 !== this.lastDirect && (this.sprite.regX = 0, this.sprite.scaleX = 1, this.lastDirect = 0);
},
  Robot.prototype.hitAndDisappear = function() {
  createjs.Tween.removeTweens(this.sprite), createjs.Tween.get(this.sprite, {override: !0}).wait(500).set({visible: !1});
},
  Robot.prototype.dropAndDisappear = function(t) {
  var e = 'undefined' == typeof t ? 0 : t ? 75 : -75;
  this.sprite.stop(), createjs.Tween.removeTweens(this.sprite), createjs.Tween.get(this.sprite, {override: !0}).to({
    x: this.sprite.x + 2 * e,
    y: this.sprite.y - 240
  }, 250).to({y: this.sprite.y + Game.stageHeight}, 800).set({visible: !1});
};
var Leaves = function(t) {
  this.config = {transThreshold: 0};
  for (var e in t) this.config[e] = t[e];
  this.moving = !1, this.lastPosY1 = 0, this.lastPosY2 = 0, this.init();
};
Leaves.prototype.init = function() {
  var t = new createjs.Bitmap(Game.loader.getResult('leaf_left')),
    e = new createjs.Bitmap(Game.loader.getResult('leaf_right'));
  t.x = 0, e.x = Game.stageWidth - e.getBounds().width, this.leafCon1 = new createjs.Container, this.leafCon1.addChild(t, e), this.leafHeight = this.leafCon1.getBounds().height, this.lastPosY1 = this.leafCon1.y = Utils.getOppoPos(this.leafHeight, 0, Game.stageHeight), this.leafCon2 = this.leafCon1.clone(!0), this.lastPosY2 = this.leafCon2.y = this.leafCon1.y - this.leafHeight, this.sprite = new createjs.Container, this.sprite.addChild(this.leafCon1, this.leafCon2);
},
  Leaves.prototype.move = function(t) {
  if (!this.moving) {
    this.moving = !0;
    var e = this.leafCon1.y;
    this.lastPosY1 = e + t;
    var i = this.leafCon2.y;
    this.lastPosY2 = i + t, createjs.Tween.removeTweens(this.leafCon1), e >= this.config.transThreshold ? this.leafCon1.y = this.lastPosY2 - this.leafHeight : createjs.Tween.get(this.leafCon1, {override: !0}).to({y: this.lastPosY1}, 500), createjs.Tween.removeTweens(this.leafCon2), i >= this.config.transThreshold ? this.leafCon2.y = this.lastPosY1 - this.leafHeight : createjs.Tween.get(this.leafCon2, {override: !0}).to({y: this.lastPosY2}, 500), this.moving = !1;
  }
};
var Floor = function(t) {
  this.config = {stairSerial: [], barrSerial: []};
  for (var e in t) this.config[e] = t[e];
  this.lastPosX = 0, this.lastPosY = 0, this.stairSerial = [], this.barrSerial = [], this.dropIndex = -1, this.stairArr = [], this.init();
};
Floor.prototype.init = function() {
  var t = new createjs.SpriteSheet({
      images: [Game.loader.getResult('spritestairs')],
      frames: [[0, 0, 150, 126], [0, 126, 170, 180], [170, 126, 170, 180], [340, 126, 170, 180], [510, 126, 170, 180], [680, 126, 170, 180]],
      animations: {stair: [0], wood: [1], landmine: [2], ice: [3], mushroom: [4], stone: [5]}
    }), e = new createjs.Container, i = new createjs.Container, s = new createjs.Container, r = new createjs.Container,
    a = new createjs.Container, n = ['stone', 'ice', 'wood', 'mushroom', 'landmine'];
  this.stair = new createjs.Sprite(t, 'stair'), this.stair.width = this.stair.getBounds().width, this.stair.height = this.stair.getBounds().height, this.barriers = [e, i, s, r, a];
  for (var o = 0; o < this.barriers.length; o++) {
    var h = new createjs.Sprite(t, n[o]), c = this.stair.clone(!0);
    h.y = c.y - 60, this.barriers[o].addChild(c, h);
  }
  this.barrCon = new createjs.Container, this.stairCon = new createjs.Container;
  var l = this.stair.clone();
  l.x = (Game.stageWidth - this.stair.width / 2) / 2, l.y = Utils.getOppoPos(this.stair.height, 300, Game.stageHeight), this.lastPosX = l.x, this.lastPosY = l.y, this.stairCon.addChild(l), this.stairArr.push(l);
  var d = this.config.stairSerial, g = this.config.barrSerial;
  this.add(d, g, !1), this.sprite = new createjs.Container, this.sprite.addChild(this.stairCon, this.barrCon);
},
  Floor.prototype.adjustZIndex = function(t) {
  function e(t, e, i) {
    return t.y > e.y ? 1 : t.y < e.y ? -1 : 0;
  }

  t.sortChildren(e);
},
  Floor.prototype._add = function(t, e, i) {
  this.stairSerial.push(t), this.barrSerial.push(e);
  var s = this.stair.clone(), r = this.lastPosX + (t ? 1 : -1) * (this.stair.width / 2),
    a = this.lastPosY - (this.stair.height - 26);
  if (s.x = r, this.stairCon.addChild(s), this.stairArr.push(s), i ? (s.y = a - 200, createjs.Tween.get(s, {override: !0}).to({y: a}, 200)) : s.y = a, 0 !== e) {
    var n = this.barriers[Utils.getRandomInt(0, this.barriers.length)].clone(!0),
      o = this.lastPosX + (t ? -1 : 1) * (this.stair.width / 2) * e, h = this.lastPosY - (this.stair.height - 26) * e;
    n.x = o, this.barrCon.addChild(n), i ? (n.y = h - 200, createjs.Tween.get(n, {override: !0}).to({y: h}, 200)) : n.y = h;
  }
  this.lastPosX = r, this.lastPosY = a;
},
  Floor.prototype.add = function(t, e, i) {
  if ('object' == typeof t) for (var s = t.length, r = 0; r < s; r++) this._add(t[r], e[r]); else this._add(t, e, i);
  this.adjustZIndex(this.stairCon), this.adjustZIndex(this.barrCon);
},
  Floor.prototype._dropStair = function(t) {
  var e = this, i = t.y;
  if (!createjs.Tween.hasActiveTweens(t)) {
    createjs.Tween.get(t, {override: !0}).to({y: i + 300}, 500).call(function() {
      e.stairCon.removeChild(t), createjs.Tween.removeTweens(t);
    });
    var s = this.barrCon.children;
    for (var r in s) {
      var a = s[r], n = a.y;
      a.y >= i && createjs.Tween.get(a, {override: !0}).to({y: n + 300}, 500).call(function() {
        e.barrCon.removeChild(a), createjs.Tween.removeTweens(a);
      });
    }
    this.dropIndex++;
  }
},
  Floor.prototype.drop = function() {
  var t = this.stairArr.shift();
  if (t && this._dropStair(t), this.stairArr.length >= 9) for (var e = this.stairArr.length - 9, i = this.stairArr.splice(0, e), s = 0; s < i.length; s++) this._dropStair(i[s]);
};
